#include "ConnectWindow.h"

void cancel_connect_cb(void *data, Evas_Object *obj, void *event_info);
void confirm_connect_cb(void *data, Evas_Object *obj, void *event_info);
void entry_clicked_cb(void* data, Evas_Object* obj, void *event_info);

ConnectWindow::ConnectWindow(Evas_Object* parent, const Network* nw):
the_nw(*nw)
{
	/*Init the Inner Window*/
	inwin = elm_win_inwin_add(parent);
	elm_win_inwin_style_set(inwin, "minimal_vertical");
	evas_object_size_hint_weight_set(inwin, 1.0, 0.0);
	evas_object_size_hint_align_set(inwin, -1.0, 0.5);
	
	/*frame*/
	persistent_frame = elm_frame_add(inwin);
	elm_frame_style_set(persistent_frame, "pad_small");
	elm_frame_label_set(persistent_frame, (CONNECT_STRING + the_nw.Name()).c_str());
	evas_object_size_hint_weight_set(persistent_frame, 1.0, 0.0);
	evas_object_size_hint_align_set(persistent_frame, 0.5, -1.0);
	elm_win_inwin_content_set(inwin, persistent_frame);
	evas_object_show(persistent_frame);
	
	/*vb*/
	vb = elm_box_add(inwin);
	evas_object_size_hint_weight_set(vb, 1.0, 1.0);
	evas_object_size_hint_align_set(vb, 0.5, -1.0);
	evas_object_show(vb);
	
	/*hb*/
	hb = elm_box_add(inwin);
	elm_box_horizontal_set(hb, true);
	evas_object_size_hint_weight_set(hb, 1.0, 1.0);
	evas_object_size_hint_align_set(vb, 0.5, -1.0);
	evas_object_show(hb);
	
	/*pad*/
	pad = elm_frame_add(inwin);
	elm_frame_style_set(pad, "pad_medium");
	evas_object_size_hint_weight_set(pad, 0.0, 0.0);
	evas_object_size_hint_align_set(pad, 0.5, -1.0);
	evas_object_show(pad);
	
	/*lbl*/
	lbl = elm_label_add(inwin);
	elm_label_label_set(lbl, "Please enter the network password");
	elm_frame_content_set(pad, lbl);
	evas_object_show(lbl);
	
	elm_box_pack_end(vb, pad);
	
	/*entry*/
	entry = elm_entry_add(inwin);
	elm_entry_single_line_set(entry, true);
	/*if(not got psk)*/		
	elm_entry_entry_set(entry, "Password");
	evas_object_size_hint_weight_set(entry, 0.0, 0.0);
	evas_object_size_hint_align_set(entry, 0.5, -1.0);
	evas_object_show(entry);
	evas_object_smart_callback_add(entry, "anchor,clicked", entry_clicked_cb, (void*) entry);
	evas_object_smart_callback_add(entry, "activated", confirm_connect_cb, (void*) this);	

	elm_box_pack_end(vb, entry);
	
	/*pad2*/
	pad2 = elm_frame_add(inwin);
	elm_frame_style_set(pad2, "pad_medium");
	evas_object_size_hint_weight_set(pad2, 1.0, 0.0);
	evas_object_size_hint_align_set(pad2, -1.0, -1.0);
	evas_object_show(pad2);
	
	/*cancel_bt*/
	cancel_bt = elm_button_add(inwin);
	elm_button_label_set(cancel_bt, "Cancel");
	evas_object_size_hint_weight_set(cancel_bt, 1.0, 1.0);
	evas_object_size_hint_align_set(cancel_bt, -1.0, -1.0);
	evas_object_show(cancel_bt);
	evas_object_smart_callback_add(cancel_bt, "clicked", cancel_connect_cb, (void*) this);
	
	elm_box_pack_end(hb, cancel_bt);
	
	/*ok_bt*/
	ok_bt = elm_button_add(inwin);
	elm_button_label_set(ok_bt, "Ok");
	evas_object_size_hint_weight_set(ok_bt, 1.0, 1.0);
	evas_object_size_hint_align_set(ok_bt, -1.0, -1.0);
	evas_object_show(ok_bt);
	evas_object_smart_callback_add(ok_bt, "clicked", confirm_connect_cb, (void*) this);
	
	elm_box_pack_end(hb, ok_bt);
	elm_frame_content_set(pad2, hb);
	elm_box_pack_end(vb, pad2);
	
	elm_frame_content_set(persistent_frame, vb);
	evas_object_show(inwin);
}

ConnectWindow::~ConnectWindow()
{
	evas_object_del(inwin);
}

void ConnectWindow::Connect()
{
	PSK(elm_entry_entry_get(entry));
	PSK( PSK().substr(0, PSK().length() - 4).c_str() );
	std::cout << "[" << ESSID() << "] " << PSK() << std::endl;
	
	output = elm_entry_add(inwin);
	elm_entry_single_line_set(output, false);
	elm_entry_line_wrap_set(output, true);
	elm_entry_editable_set(output, false);
	evas_object_size_hint_weight_set(output, 1.0, 1.0);
	evas_object_size_hint_align_set(output, 0.5, -1.0);
	evas_object_show(output);
	
	elm_frame_content_set(persistent_frame, output);
	evas_object_del(vb);
	
	std::string o = "<b>Connecting to <em>";
	o += ESSID();
	o += "</></><br>";
	elm_entry_entry_insert(output, o.c_str());
	elm_entry_entry_insert(output, "</>");
	ecore_timer_add(0.25, clean_interface, (void*) output);
} 

void entry_clicked_cb(void* data, Evas_Object* obj, void *event_info)
{
	elm_entry_select_all((Evas_Object*) data);
}

void cancel_connect_cb(void *data, Evas_Object *obj, void *event_info)
{
	std::cout << "Cancel!" << std::endl;
	ConnectWindow* cw = (ConnectWindow*) data;
	delete cw;
}

void confirm_connect_cb(void *data, Evas_Object *obj, void *event_info)
{
	std::cout << "Confirm!" << std::endl;
	((ConnectWindow*) data)->Connect();
}

int clean_interface(void* output)
{
	elm_entry_entry_insert((Evas_Object*) output, "Preparing interface...<br>");
	POpen po("ifconfig eth0 down && iwconfig eth0 txpower off channel 0 && killall wpa_supplicant && killall udhcpc");
	po.GetData();
	ecore_timer_add(0.25, generate_wpa_supplicant, output);
	return ECORE_CALLBACK_CANCEL;
}

int generate_wpa_supplicant(void* output)
{
	elm_entry_entry_insert((Evas_Object*) output, "Generating wpa_supplicant.conf...<br>");
	
	ecore_timer_add(0.25, run_wpa_supplicant, output);
	return ECORE_CALLBACK_CANCEL;
}

int run_wpa_supplicant(void* output)
{
	elm_entry_entry_insert((Evas_Object*) output, "Running wpa_supplicant...<br>");
	
	ecore_timer_add(2, run_udhcpc, output);
	return ECORE_CALLBACK_CANCEL;
}

int run_udhcpc(void* output)
{
	elm_entry_entry_insert((Evas_Object*) output, "Running udhcpc...<br>");
	return ECORE_CALLBACK_CANCEL;
}
